package com.qqbot.service.group;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qqbot.enums.StatusEnum;
import com.qqbot.exception.BusinessException;
import com.qqbot.model.group.Groups;
import com.qqbot.dao.group.GroupsDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qqbot.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 群信息 服务实现类
 * </p>
 *
 * @author fyq
 * @since 2023-03-03
 */
@Service
public class GroupsServiceImpl extends ServiceImpl<GroupsDao, Groups> implements GroupsService {

    @Override
    public boolean isItEnable(String bot, String groupNo) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, groupNo)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<Groups> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("group_no", groupNo);
        query.eq("status", StatusEnum.TRUE.getCode());
        return this.count(query) > 0;
    }

    @Override
    public boolean saveGroup(String bot, String groupNo, Integer status) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, groupNo) || Objects.isNull(status)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<Groups> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("group_no", groupNo);
        Groups group = this.getOne(query);
        if (Objects.nonNull(group)) {
            return false;
        }
        group = new Groups();
        group.setBot(bot);
        group.setGroupNo(groupNo);
        group.setStatus(status);
        group.setWelcomeContent("欢迎新成员");
        return this.save(group);
    }

    @Override
    public Groups getByGroupNo(String bot, String groupNo) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, groupNo)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<Groups> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("group_no", groupNo);
        query.eq("status", StatusEnum.TRUE.getCode());
        return this.getOne(query, false);
    }
}
